#define _CRT_SECURE_NO_WARNINGS
#include "Lily_help.h"
#ifdef in_PC
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
using namespace std;
using namespace chrono;

#define var auto

static system_clock::time_point _start_time;
unsigned int li_millis()
{
    var now = system_clock::now();
    std::chrono::duration<double, std::milli> dur = now - _start_time;
    auto int_ms = std::chrono::duration_cast<std::chrono::milliseconds>(dur);
    return int_ms.count();
}

void *_out_to_screen(void *arg, const char *s, int len)
{
    cout << s;
    return NULL;
}
void li_out_(const char *s)
{
    _out_to_screen(NULL, s, -1);
}
Pipe std_pipe;
bool _need_to_exit = false;
extern "C"
{

}
#if _WIN32
#include <windows.h>
#define GREEN 2
#define RED 4
#define BLUE 1
#define BLACK 0
void SetColor(unsigned short forecolor =7 ,unsigned short backgroudcolor =0)
{
	HANDLE hCon =GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hCon,forecolor|backgroudcolor);
}
#elif __linux__
#define GREEN 32
#define RED 31
#define BLUE 34
#define BLACK 30

void SetColor(unsigned short forecolor =0 ,unsigned short backgroudcolor =0)
{
    cout<<"\033[1"<<";"<<forecolor<<"m";
}
#endif
int tf_inputs()
{
    while (!_need_to_exit)
    {
        string s;
        SetColor(GREEN,BLACK);
        cout << ">>";
        SetColor();
        getline(cin,s);
        if(_need_to_exit)break;
        s.append("\n");
        pipe_release_data(std_pipe, (char *)(s.c_str()), s.length());
    }
    return 0;
}

int tf_tick()
{
    while (!_need_to_exit)
    {
        this_thread::sleep_for(microseconds(10000));//10ms
        lily_tick();
    }
    return 0;
}
int f_exit()
{
    _need_to_exit = true;
    return 0;
}

int main()
{
    _start_time = system_clock::now();
    const int ax = sizeof(int);
    cout<<"Arc: ";
    if (ax == 4)
    {
        cout << "x86" << endl;
    }
    else if (ax == 8)
    {
        cout << "x64" << endl;
    }
    cout<<__DATE__<<" "<<__TIME__<<endl;
    cout<<"Hello, this's "<<_VersionMsg<<endl;
    Tasks_def task_p;
    _li_Delay = [](uint32_t ms)
    { this_thread::sleep_for(milliseconds(ms)); };
    lily_init();
    std_pipe = li_new_output_device("std", NULL, (PipeLine)_out_to_screen);
    Pipe in_to_lily = lily_pipe_io_setup(std_pipe);
    pipe_connect_to(std_pipe, in_to_lily);
    _li_millis = li_millis;
    public_a_fun_link_int("exit", (void*)f_exit, 0);

    thread t_input(tf_inputs);
    thread t_tick(tf_tick);

    li_out_("type help to see doc\n");

    while (!_need_to_exit)
    {
        tp_run_tasks_once();
        this_thread::sleep_for(microseconds(5));
    }
    t_tick.join();
    cout<<"type anything to exit"<<endl<<">>";
    t_input.join();
    return 0;
}
#endif